using System;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;

namespace SdmxMl.Pdf
{
public class PdfWriterEvents : IPdfPageEvent
    {
        string watermarkText = string.Empty;

        public PdfWriterEvents(string watermark) 
        {
            watermarkText = watermark;
        }

        public void OnOpenDocument(PdfWriter writer, Document document) { }
        public void OnCloseDocument(PdfWriter writer, Document document) { }

        public void OnStartPage(PdfWriter writer, Document document) 
        {
            /*
            float fontSize = 80;
            float xPosition = 300;
            float yPosition = 400;
            float angle = 45;
            try
            {
                PdfContentByte under = writer.DirectContentUnder;
                BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
                under.BeginText();
                under.SetColorFill(BaseColor.LIGHT_GRAY);
                under.SetFontAndSize(baseFont, fontSize);
                under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
                under.EndText();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
             * */
        }
        public void OnEndPage(PdfWriter writer, Document document) { }
        public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition) { }
        public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) { }
        public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { }
        public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition) { }
        public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) { }
        public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition) { }
        public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string String) { }

    }

    public class PdfWriterPage : iTextSharp.text.pdf.PdfPageEventHelper
   {
        private string agencyText;
        private int noPage;

        public PdfWriterPage(string agencyId)
        {
            agencyText = agencyId;
            noPage = 1;
        }

       //I create a font object to use within my footer
      protected Font footer
      {
          get
          {
               // create a basecolor to use for the footer font, if needed.
               BaseColor grey = new BaseColor(128, 128, 128);
               Font font = FontFactory.GetFont("Arial", 8, grey);
               return font;
          }
      }
		
		protected Font header
      {
          get
          {
               // create a basecolor to use for the footer font, if needed.
               //BaseColor grey = new BaseColor(128, 128, 128);
               Font font = FontFactory.GetFont("Arial", 8);
               return font;
          }
      }
       //override the OnStartPage event handler to add our header
       public override void OnStartPage(PdfWriter writer, Document doc)
       {
            //I use a PdfPtable with 1 column to position my header where I want it
            PdfPTable headerTbl = new PdfPTable(2);

            //set the width of the table to be the same as the document
            headerTbl.TotalWidth = doc.PageSize.Width-144;
			headerTbl.HorizontalAlignment = Element.ALIGN_RIGHT;
			
			//I use an image logo in the header so I need to get an instance of the image to be able to insert it. I believe this is something you couldn't do with older versions of iTextSharp
            // Image logo = Image.GetInstance(HttpContext.Current.Server.MapPath("/images/logo.jpg"));

            //I used a large version of the logo to maintain the quality when the size was reduced. I guess you could reduce the size manually and use a smaller version, but I used iTextSharp to reduce the scale. As you can see, I reduced it down to 7% of original size.
            // logo.ScalePercent(7);

            //create instance of a table cell to contain the logo
            PdfPCell cellA = new PdfPCell(new Phrase(agencyText, header));
			PdfPCell cellB = new PdfPCell(new Phrase(DateTime.Now.ToString(), header));

            //align the logo to the right of the cell
            cellA.HorizontalAlignment = Element.ALIGN_LEFT;

            //add a bit of padding to bring it away from the right edge
            //cell.Padding = 20;

            //remove the border
            cellA.Border=2;
			cellB.Border=2;

            //Add the cell to the table
            headerTbl.AddCell(cellA);
			
			cellB.HorizontalAlignment = Element.ALIGN_RIGHT;
			headerTbl.AddCell(cellB);
			//headerTbl.AddCell("");

            //write the rows out to the PDF output stream. I use the height of the document to position the table. Positioning seems quite strange in iTextSharp and caused me the biggest headache.. It almost seems like it starts from the bottom of the page and works up to the top, so you may ned to play around with this.
            headerTbl.WriteSelectedRows(0,-1, 72, (doc.PageSize.Height-50), writer.DirectContent);
       }

       //override the OnPageEnd event handler to add our footer
       public override void OnEndPage(PdfWriter writer, Document doc)
       {
            //I use a PdfPtable with 2 columns to position my footer where I want it
            PdfPTable footerTbl = new PdfPTable(2);

            //set the width of the table to be the same as the document
            footerTbl.TotalWidth = doc.PageSize.Width-144;

            //Center the table on the page
            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

            //Create a paragraph that contains the footer text
            Paragraph para = new Paragraph("Generated by SDMX SAE Editor", footer);

            //add a carriage return
            para.Add(Environment.NewLine);
            para.Add("www.sdmx.ch");

            //create a cell instance to hold the text
            PdfPCell cell = new PdfPCell(para);

            //set cell border to 0
            cell.Border = 0;

            //add some padding to bring away from the edge
            cell.PaddingLeft = 0;

            //add cell to table
            footerTbl.AddCell(cell);

            //create new instance of Paragraph for 2nd cell text
            para = new Paragraph((this.noPage++).ToString(), footer);

           //create new instance of cell to hold the text
            cell = new PdfPCell(para);

            //align the text to the right of the cell
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            //set border to 0
            cell.Border = 0;

            // add some padding to take away from the edge of the page
            cell.PaddingRight = 0;

            //add the cell to the table
            footerTbl.AddCell(cell);

         //write the rows out to the PDF output stream.
         footerTbl.WriteSelectedRows(0,-1, 72, (doc.BottomMargin-10),  writer.DirectContent);
       }
   }

}